<?php

namespace app\advertisement\controller;

use app\common\controller\PCBase;
// use app\common\controller\DBase;//测试专用
use app\advertisement\logic\AdvaccountlogLogic;
use app\advertisement\model\Advaccountlog as AdvaccountlogModel;
use think\facade\Db;
use Throwable;
use app\common\library\Upload;

class Advaccountlog extends PCBase
{
    /**
     * 模型
     * @var object
     * @phpstan-var AdvaccountlogModel
     */
    protected object $model;

    public function initialize(): void
    {
        parent::initialize();
        $this->model = new AdvaccountlogModel();
    }

    /**
     * index
     * @throws Throwable
     */
    public function index()
    {
        $params     = $this->request->post();
        $AdvaccountlogLogic = new AdvaccountlogLogic();
        return $AdvaccountlogLogic->index($params);
    }


    /**
     * 添加
     * @throws Throwable
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if ($this->modelValidate) {
                try {
                    $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                    $validate = new $validate();
                    $validate->scene('edit')->check($data);
                } catch (Throwable $e) {
                    return json(DefaultResponse(201, false, $e->getMessage(), null));
                }
            }

            $AdvaccountlogLogic = new AdvaccountlogLogic();
            $result = $AdvaccountlogLogic->add($data);
            return $result;
        }
    }

    /**
     * 编辑
     * @throws Throwable
     */
    public function edit($id = null)
    {
        $row = $this->model->find($id);
        if (!$row) {
            return json(DefaultResponse(201, false, '记录不能为空!', null));
        }

        if ($this->request->isPost()) {
            $data = $this->request->post();
            if ($this->modelValidate) {
                try {
                    $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                    $validate = new $validate();
                    $validate->scene('edit')->check($data);
                } catch (Throwable $e) {
                    return json(DefaultResponse(201, false, $e->getMessage(), null));
                }
            }

            $AdvaccountlogLogic = new AdvaccountlogLogic();
            $result = $AdvaccountlogLogic->edit($row, $data);

            return $result;
        }
        //回显
        if(!empty($row['createtime'])){
            $row['createtime'] = date('Y-m-d H:i:s', $row['createtime']);
        }
        if(!empty($row['updatetime'])){
            $row['updatetime'] = date('Y-m-d H:i:s', $row['updatetime']);
        }
        if(!empty($row['advertiser_id'])){
            $row['advertiser_id'] = (int)$row['advertiser_id'];
        }
        return json(DefaultResponse(200, true, '获取成功', ['row' => $row]));
    }
}
